<?php
namespace App\Service\ApiXcx;

use Illuminate\Support\Facades\DB;

class IndexService extends BaseService
{
    //首页

    /**
     * 品牌
     * @return array
     */
    public function getBrandIndex()
    {
        $arys = ['top' => [], 'top2' => [], 'middle' => [], 'bottom' => []];
        $res = DB::table($this->dbBrand)
            ->where('del', 0)
            ->where('is_show', 1)
            ->get();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => $arys);
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $img = $v['cover'] ? env('DOMAIN') . '/'. $v['cover'] : '';
            $pp = [
                'id' => $v['id'],
                'cover' => $img,
                'gid' => $v['gid'],
            ];
            if ($v['type'] == 1) {
                $arys['top'][] = $pp;
            }
            if ($v['type'] == 3) {
                $arys['bottom'][] = $pp;
            }
            if ($v['type'] == 4) {
                $arys['top2'][] = $pp;
            }
        }
//        $arys['middle'] = [
//            'title' => '购物金限时优惠',
//            'full' => 1000,
//            'rebate' => 1500,
//            'small' => '*购物金可与优惠劵叠加使用',
//            'remark' => '（注：单笔订单只能使用一张优惠劵）',
//        ];
        //获取会员劵
        $couponOne = (array) $this->dbConn($this->dbUcouponTmp)
            ->where('del', 0)
            ->orderBy('quata', 'desc')
            ->first();
        $arys['middle'] = [
            'title' => '购物金限时优惠',
            'full' => empty($couponOne) ? 0 : toNumber($couponOne['full']),
            'rebate' => empty($couponOne) ? 0 : toNumber($couponOne['reduce']),
            'small' => '*购物金可与优惠劵叠加使用',
            'remark' => '（注：单笔订单只能使用一张优惠劵）',
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 品牌
     * @return array
     */
    public function getBrandList()
    {
        $res = $this->dbConn($this->dbBrand)
            ->where('del', 0)
            ->where('is_show', 1)
            ->get();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => $arys);
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $img = $v['cover'] ? env('DOMAIN') . '/'. $v['cover'] : '';
            if (in_array($v['type'], [1, 3, 4])) {
                $arys[] = [
                    'id' => $v['id'],
                    'cover' => $img,
                    'gid' => $v['gid'],
                    'type' => $v['type'],
                    'title' => '',
                    'full' => 0,
                    'rebate' => 0,
                    'small' => '',
                    'remark' => '',
                ];
            }
        }
        //获取会员劵
        $couponOne = (array) $this->dbConn($this->dbUcouponTmp)
            ->where('del', 0)
            ->orderBy('quata', 'desc')
            ->first();
        $arys[] = [
            'id' => 0,
            'cover' => '',
            'gid' => 0,
            'type' => 2,
            'title' => '购物金限时优惠',
            'full' => empty($couponOne) ? 0 : toNumber($couponOne['full']),
            'rebate' => empty($couponOne) ? 0 : toNumber($couponOne['reduce']),
            'small' => '*购物金可与优惠劵叠加使用',
            'remark' => '（注：单笔订单只能使用一张优惠劵）',
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 品牌
     * @return array
     */
    public function getBrandOne($id)
    {
        $res = (array) DB::table($this->dbBrand)->where('del', 0)->where('id', $id)->first();
        if (empty($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => []);
        }
        $arys = [
            'id' => $res['id'],
            'cover' => env('DOMAIN') . '/' . $res['cover'],
            'detail' => htmlspecialchars_decode($res['detail']),
            'gid' => $res['gid'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    //轮播图
    public function bannerIndex()
    {
        $res = $this->dbConn($this->dbQdBanner)
            ->where('del', 0)
            ->where('type', 2)
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            if (file_exists($v['link'])) {
                $arys[] = env('DOMAIN').'/'.$v['link'];
            }
        }
        if (empty($arys)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $arys, count($arys));
    }

    //轮播图
    public function findCateList($pid = 0)
    {
        $db = $this->dbConn($this->dbFindCate)->where('del', 0);
        if ($pid) {
            $db = $db->where('parent_id', $pid);
        }
        $res = $db->select('id', 'cate_name as title')->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $res);
    }

    //发现页面开关
    public function findSwitch()
    {
        $res = (array) $this->dbwbConn($this->dbQdConfig)->where('id', 11)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [
            'switch' => $res['value']==1 ? true : false,
        ];
        return $this->toback(0, '', $arys);
    }
}